{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "cdf9e03b-a513-4b81-a75e-243f38a3c84a",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e2cc6eb4",
   "metadata": {},
   "source": [
    "### clone代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb4a2c42",
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8e90fe29",
   "metadata": {},
   "source": [
    "### 安装特定依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c1ede08-857f-4592-9093-d7e5a37ce245",
   "metadata": {},
   "outputs": [],
   "source": [
    "!cd modelscope-agent && pip install -r requirements.txt"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "9794ba6c-fa1a-43f2-a2a6-86f35fec0ba1",
   "metadata": {},
   "source": [
    "### 下载本地语料库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96e36b08-5ab8-4905-aaea-15621039a951",
   "metadata": {},
   "outputs": [],
   "source": [
    "!wget -P /root/you_data/ https://modelscope.oss-cn-beijing.aliyuncs.com/resource/agent/modelscope_qa.txt "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22ea836b",
   "metadata": {},
   "source": [
    "### 本地配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63027bb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/examples/memory & knowledge')\n",
    "\n",
    "import sys\n",
    "sys.path.append('../../')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fe3acde",
   "metadata": {},
   "source": [
    "### API_KEY管理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "095b2539",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "print('请输入DASHSCOPE_API_KEY')\n",
    "os.environ['DASHSCOPE_API_KEY'] = input()\n",
    "print('请输入ModelScope Token')\n",
    "os.environ['MODELSCOPE_API_TOKEN'] = input()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d8f16de",
   "metadata": {},
   "source": [
    "### 构建agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7547df63",
   "metadata": {},
   "outputs": [],
   "source": [
    "from modelscope_agent.agents import RolePlay\n",
    "\n",
    "role_template = '你扮演一个python专家，需要给出解决方案'\n",
    "\n",
    "llm_config = {\n",
    "    'model': 'qwen-max', \n",
    "    'model_server': 'dashscope',\n",
    "    }\n",
    "\n",
    "function_list = []\n",
    "\n",
    "bot = RolePlay(function_list=function_list, llm=llm_config, instruction=role_template)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8016347c",
   "metadata": {},
   "source": [
    "### 构建memory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa4d0a7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from modelscope_agent.memory import MemoryWithRetrievalKnowledge\n",
    "\n",
    "DEFAULT_UUID_HISTORY = \"/root/you_data/you_config/history\"\n",
    "storage_path = \"/root/you_data/you_config/config\"\n",
    "memory_history_path = os.path.join(DEFAULT_UUID_HISTORY, 'default_user.json')\n",
    "memory_agent_name = 'default_memory'\n",
    "memory = MemoryWithRetrievalKnowledge(storage_path=storage_path,\n",
    "        name=memory_agent_name,\n",
    "        memory_path=memory_history_path,\n",
    "        )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff1481b3",
   "metadata": {},
   "source": [
    "### 执行agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7292596f",
   "metadata": {},
   "outputs": [],
   "source": [
    "ref_doc = memory.run(\n",
    "            query=\"pip install的时候有些包下载特别慢怎么办\", url=\"/root/you_data/modelscope_qa.txt\", checked=True)\n",
    "response = bot.run(\"pip install的时候有些包下载特别慢怎么办\", remote=False, print_info=True, ref_doc=ref_doc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e167ee24",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = ''\n",
    "for chunk in response:\n",
    "    text += chunk\n",
    "print(text)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
